package cn.afactory.tygz.ui.sns.controller;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.cache.LocalJsonCache;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.controller.BaseLogicController;
import cn.afactory.tygz.controller.CommunityController;
import cn.afactory.tygz.model.ImageInfoList;
import cn.afactory.tygz.model.NearByUserInfoList;
import cn.afactory.tygz.model.PrasizeResult;
import cn.afactory.tygz.model.SnsCommentInfoList;
import cn.afactory.tygz.model.SnsSubjectDetail;
import cn.afactory.tygz.model.SnsSubjectDetailList;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.model.SnsTopicInfoList;
import cn.afactory.tygz.model.SnsUserInfo;
import cn.afactory.tygz.model.SnsUserInfoList;
import cn.afactory.tygz.model.UploadFileInfoList;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.adapter.NearByForumsAdapter;
import cn.afactory.tygz.ui.base.adapter.SubjectAgreeUserGirdAdapter;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.base.utils.sns.ViewTools;
import cn.afactory.tygz.ui.base.views.AutoGridView;


public class SnsControler extends BaseLogicController {
    public static final int MSG_DATA_FROM_CACHE =-1000;
    public static final int MSG_SUBJECTS_LOADED_OK = 1000;
    public static final int MSG_SUBJECTS_LOADED_KO = 1001;
    public static final int MSG_RELOAD_SUBJECTS = 1110;
    public static final int MSG_CHECK_HOT_SUBJECTS_NEW = 1211;
    public static final String ACTION_RELOAD_SUBJECTS = "action_reload_subjects";
    public static final String ACTION_RELOAD_COMMENTS = "action_reload_comments";
    public static final int MSG_FANS_LOADED_OK = 1301;
    public static final int MSG_FANS_LOADED_KO = 1302;
    public static final int MSG_RELOAD_FANS = 1303;
    public static final int MSG_FOLLOWED_FANS_LOADED_OK = 1304;
    public static final int MSG_FOLLOWED_FANS_LOADED_KO = 1305;
    public static final int MSG_RELOAD_FOLLOWED_FANS = 1306;
    public static final int MSG_FOLLOWED_USER_OK = 1307;
    public static final int MSG_FOLLOWED_USER_KO = 1308;
    public static final int MSG_UNFOLLOWED_USER_OK = 1309;
    public static final int MSG_UNFOLLOWED_USER_KO = 1310;
    public static final int MSG_FOLLOWED_TOPIC_OK = 1309;
    public static final int MSG_FOLLOWED_TOPIC_KO = 1310;
    public static final int MSG_UNFOLLOWED_TOPIC_OK = 1311;
    public static final int MSG_UNFOLLOWED_TOPIC_KO = 1312;

    public static final int MSG_NEAR_BY_FORUMS_OK = 1401;
    public static final int MSG_NEAR_BY_FORUMS_KO = 1402;

    public static final int MSG_NEAR_BY_PEOPLES_OK = 1404;
    public static final int MSG_NEAR_BY_PEOPLES_KO = 1405;

    //点赞列表
    public static final int MSG_GET_PRASIZE_LIST_OK = 1407;
    public static final int MSG_GET_PRASIZE_LIST_KO = 1408;

    //获取话题列表
    public static final int MSG_GET_TOPIC_LIST_OK = 1410;
    public static final int MSG_GET_TOPIC_LIST_KO = 1411;
    //黑名单列表
    public static final int MSG_GET_BLACK_LIST_OK = 1413;
    public static final int MSG_GET_BLACK_LIST_KO = 1414;

    public static final int MSG_GET_FORUMS_DETAIL_OK = 1415;
    public static final int MSG_GET_FORUMS_DETAIL_KO = 1416;
    public static final int MSG_GET_USER_INFO_OK = 1417;
    public static final int MSG_GET_USER_INFO_KO = 1418;
    public static final int MSG_GET_USER_ALBUMS_OK = 1419;
    public static final int MSG_GET_USER_ALBUMS_KO = 1420;

    public static final int MSG_GET_FORUMS_COMMENTS_OK = 1421;
    public static final int MSG_GET_FORUMS_COMMENTS_KO = 1422;
    public static final int MSG_GET_USER_FORUMS_OK = 1423;
    public static final int MSG_GET_USER_FORUMS_KO = 1424;

    public static final int MSG_GET_TOPIC_COMMENT_LIST_OK = 1425;
    public static final int MSG_GET_TOPIC_COMMENT_LIST_KO = 1426;
    public static final int MSG_GET_TOPIC_DETAIL_OK = 1427;
    public static final int MSG_GET_TOPIC_DETAIL_KO = 1428;

    //拉黑
    public static final int MSG_BLACK_USER_OK = 1501;
    public static final int MSG_BLACK_USER_KO = 1502;

    //取消拉黑
    public static final int MSG_UNBLACK_USER_OK = 1504;
    public static final int MSG_UNBLACK_USER_KO = 1505;
    //发布
    public static final int MSG_PUBLISH_SUBJECT_OK = 1600;
    public static final int MSG_PUBLISH_SUBJECT_KO = 1601;

    private static String TAG;
    private CacheManager mCacheManager ;

    public SnsControler(Context context, Handler handler, String CacheTag) {
        super(context, handler);
        TAG = mContext.getClass().getSimpleName();
        mCacheManager = new CacheManager(!TextUtils.isEmpty(CacheTag) ? CacheTag : "");
    }

    public void publishSubject(String content, long topicId, int mFileFiledId){
        NetManager.getInstance(mContext).doPublishForum(content,topicId, mFileFiledId,new OnResponseListener<Boolean>() {
            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_PUBLISH_SUBJECT_KO, errorCode, 0, errorMessage);
            }

            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if(isOK && result){
                    sendMessage(MSG_PUBLISH_SUBJECT_OK, result);
                }else{
                    sendMessage(MSG_PUBLISH_SUBJECT_KO, errorCode, 0, errorMsg);
                }
            }
        });
    }

    /**
     * 加载附近的动态
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadNearByForumsData(final int mPageIndex,final int mPageSize) {
//        int pageIndex = mPageIndex + 1;
//        int pageSize = mPageSize;

        NetManager.getInstance(mContext).doGetNearByForumsList(mPageIndex,
                mPageSize,
                new OnResponseListener<SnsSubjectDetailList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_NEAR_BY_FORUMS_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsSubjectDetailList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsSubjectDetailList();
                            }
                            if (mPageIndex == 1) {
                                mCacheManager.saveSnsSubjectDetailList(result);
                            }
                            sendMessage(MSG_NEAR_BY_FORUMS_OK, result);
                            return;
                        }
                        sendMessage(MSG_NEAR_BY_FORUMS_KO, errorCode, 0, null);
                    }
                }
        );
    }

    /**
     * 获取附近的人
     * @param sex
     * @param startAge
     * @param endAge
     * @param cityCode
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadNearByPeoplesData(final int sex,
                                      final int startAge,
                                      final int endAge,
                                      final int cityCode,
                                      final int mPageIndex,
                                      final int mPageSize) {
        int pageIndex = mPageIndex + 1;
        int pageSize = mPageSize;
        NetManager.getInstance(mContext).doGetNearByPeoplesList(
                sex,
                startAge,
                endAge,
                cityCode,
                pageIndex,
                pageSize,
                new OnResponseListener<NearByUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_NEAR_BY_PEOPLES_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, NearByUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new NearByUserInfoList();
                            }
                            sendMessage(MSG_NEAR_BY_PEOPLES_OK, result);
                            return;
                        }
                        sendMessage(MSG_NEAR_BY_PEOPLES_KO, errorCode, 0, null);
                    }
                }
        );
    }

    /**
     * 获取帖子点赞列表
     * @param postId
     * @param page
     * @param row
     */
    public void doGetPrasizeUserList(final long postId, final int page, final int row) {
        NetManager.getInstance(mContext).doGetPrasizeUserList(postId,
                page,
                row,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_GET_PRASIZE_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_GET_PRASIZE_LIST_OK, result);
                            return;
                        }
                        sendMessage(MSG_GET_PRASIZE_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 给话题点赞
     * @param v
     * @param deptInfo
     */
    public void doPraiseToSubject(final AutoGridView autoGridView,
                                  final TextView v,
                                  final SnsSubjectDetail deptInfo) {
        if(deptInfo == null ){
            return;
        }
        if (deptInfo.isAgree == 1) {
            return;
        }
        NetManager.getInstance(mContext).doPrasize(deptInfo.id,
                new OnResponseListener<PrasizeResult>() {
                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        LocalUtils.showToast(mContext, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, PrasizeResult result, int errorCode, String errorMsg) {
                        if (isOK) {
                            ViewTools.setPraiseView(v, true, deptInfo.agree = deptInfo.agree + 1);
                            deptInfo.isAgree = 1;
                            //增加点赞列表
                            SnsUserInfo snsUserInfo = new SnsUserInfo();
                            snsUserInfo.userIcon = SPUtils.getUserIcon(mContext);
                            deptInfo.agreeUser.add(0, snsUserInfo);
                            if (null != deptInfo.agreeUser && deptInfo.agreeUser.size() > 0) {
                                int mIconWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.agree_people_icon_width);
                                int mIconHeight = mIconWidth + mContext.getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
                                autoGridView.setVisibility(View.VISIBLE);
                                SubjectAgreeUserGirdAdapter mAgreeUserGirdAdapter = new SubjectAgreeUserGirdAdapter(mContext, deptInfo.agreeUser, mIconWidth, mIconHeight);
                                autoGridView.setAdapter(mAgreeUserGirdAdapter);

                                autoGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        NaviUtils.gotoUserList(mContext,
                                                -1,
                                                null,
                                                deptInfo.id,
                                                deptInfo.agree,
                                                UserListType.PRASIZE_LIST);
                                    }
                                });
                            }
//                            LocalUtils.showToast(mContext, mContext.getString(R.string.health_praise_comment_ok));
                        } else {
                            LocalUtils.showToast(mContext, errorMsg);
                        }
                    }
                }
        );
    }

    /**
     * 给话题点赞
     * @param v
     * @param deptInfo
     */
    public void doPraiseToSubject(
                                  final TextView v,
                                  final SnsSubjectDetail deptInfo) {
        if(deptInfo == null ){
            return;
        }
        if (deptInfo.isAgree == 1) {
            return;
        }
        NetManager.getInstance(mContext).doPrasize(deptInfo.id,
                new OnResponseListener<PrasizeResult>() {
                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        LocalUtils.showToast(mContext, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, PrasizeResult result, int errorCode, String errorMsg) {
                        if (isOK) {
                            ViewTools.setPraiseView(v, true, deptInfo.agree = deptInfo.agree + 1);
                            deptInfo.isAgree = 1;
//                            LocalUtils.showToast(mContext, mContext.getString(R.string.health_praise_comment_ok));
                        } else {
                            LocalUtils.showToast(mContext, errorMsg);
                        }
                    }
                }
        );
    }


    /**
     * 获取我的粉丝列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doLoadFansUserList(final long targerUserId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetFriendsList(
                targerUserId,
                mPageIndex,
                mPageSize,
                1,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_FANS_LOADED_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_FANS_LOADED_OK, result);
                            return;
                        }
                        sendMessage(MSG_FANS_LOADED_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 获取我关注的用户列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doLoadFollowedUserList(final long targetUserId,
                                   final int mPageIndex,
                                   final int mPageSize) {
        NetManager.getInstance(mContext).doGetFriendsList(
                targetUserId,
                mPageIndex,
                mPageSize,
                0,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_FOLLOWED_FANS_LOADED_OK, result);
                            return;
                        }
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, null);
                    }
                }
        );
    }

    /**
     * 获取公司用户列表
     * @param companyId
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetCompanyUserList(final long companyId,
                                       final int mPageIndex,
                                     final int mPageSize) {
        NetManager.getInstance(mContext).doGetCompanyUserList(
                companyId,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_FOLLOWED_FANS_LOADED_OK, result);
                            return;
                        }
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, null);
                    }
                }
        );
    }

    /**
     * 获取关注话题用户列表
     * @param topicId
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetFocusTopicUserList(final long topicId,
                                     final int mPageIndex,
                                     final int mPageSize) {
        NetManager.getInstance(mContext).doGetFocusTopicUserList(
                topicId,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_FOLLOWED_FANS_LOADED_OK, result);
                            return;
                        }
                        sendMessage(MSG_FOLLOWED_FANS_LOADED_KO, errorCode, 0, null);
                    }
                }
        );
    }

    /**
     * 关注用户
     */
    public void doFollowUser(final TextView tv, final SnsUserInfo snsUserInfo) {
        NetManager.getInstance(mContext).doFocusUser(snsUserInfo.userId, 0, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_FOLLOWED_USER_OK, result);
                    snsUserInfo.isAttentioned = true;
                    tv.setText(mContext.getString(R.string.label_btn_cancel_attention));
                } else {
//                    sendMessage(MSG_FOLLOWED_USER_KO, errorCode, 0, errorMsg);
                    LocalUtils.showToast(mContext, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
//                sendMessage(MSG_FOLLOWED_USER_KO, errorCode, 0, errorMessage);
                LocalUtils.showToast(mContext, errorMessage);
            }
        });
    }
    /**
     * 关注用户
     */
    public void doFollowUser(long userId) {
        NetManager.getInstance(mContext).doFocusUser(userId, 0, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_FOLLOWED_USER_OK, result);
                } else {
                    sendMessage(MSG_FOLLOWED_USER_KO, errorCode, 0, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_FOLLOWED_USER_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 取消关注用户
     */
    public void doUnFollowUser(final TextView tv ,final SnsUserInfo snsUserInfo) {
        NetManager.getInstance(mContext).doFocusUser(snsUserInfo.userId, 1, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_UNFOLLOWED_USER_OK, result);
                    snsUserInfo.isAttentioned = false;
                    tv.setText(mContext.getString(R.string.label_btn_attention));
                } else {
//                    sendMessage(MSG_UNFOLLOWED_USER_KO, errorCode, 0, errorMsg);
                    LocalUtils.showToast(mContext, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
//                sendMessage(MSG_UNFOLLOWED_USER_KO, errorCode, 0, errorMessage);
                LocalUtils.showToast(mContext, errorMessage);
            }
        });
    }
    /**
     * 取消关注用户
     */
    public void doUnFollowUser(long userId) {
        NetManager.getInstance(mContext).doFocusUser(userId, 1, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_UNFOLLOWED_USER_OK, result);
                } else {
                    sendMessage(MSG_UNFOLLOWED_USER_KO, errorCode, 0, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_UNFOLLOWED_USER_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取用户参与的帖子列表
     * @param userId
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadUserJoinedForumsList(final long userId, final int mPageIndex, final int mPageSize) {

        NetManager.getInstance(mContext).doGetMyJoinedForumSList(userId, mPageIndex, mPageSize, new OnResponseListener<SnsSubjectDetailList>() {
            @Override
            public void onComplete(boolean isOK, SnsSubjectDetailList result, int errorCode, String errorMsg) {
                if (isOK) {
                    if (result == null) {
                        result = new SnsSubjectDetailList();
                    }
                    sendMessage(MSG_GET_USER_FORUMS_OK, result);
                    return;
                }
                sendMessage(MSG_GET_USER_FORUMS_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_USER_FORUMS_KO, errorCode, 0, errorMessage);
            }
        });

    }

    /**
     * 获取我发布的帖子列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetMyPostList(final int mPageIndex, final int mPageSize) {

        NetManager.getInstance(mContext).doGetMyPostList(mPageIndex, mPageSize, new OnResponseListener<SnsSubjectDetailList>() {
            @Override
            public void onComplete(boolean isOK, SnsSubjectDetailList result, int errorCode, String errorMsg) {
                if (isOK) {
                    if (result == null) {
                        result = new SnsSubjectDetailList();
                    }
                    sendMessage(MSG_GET_USER_FORUMS_OK, result);
                    return;
                }
                sendMessage(MSG_GET_USER_FORUMS_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_USER_FORUMS_KO, errorCode, 0, errorMessage);
            }
        });

    }

    /**
     * 获取话题列表
     * @param targerUserId
     * @param type
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetMyTopicList(final long targerUserId, final int type, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetMyTopicList(
                targerUserId,
                type,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsTopicInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_GET_TOPIC_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsTopicInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsTopicInfoList();
                            }
                            sendMessage(MSG_GET_TOPIC_LIST_OK, result);
                            return;
                        }
                        sendMessage(MSG_GET_TOPIC_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 获取黑名单列表
     * @param targerUserId
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetBlackList(final long targerUserId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetBlackList(
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsUserInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_GET_BLACK_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsUserInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsUserInfoList();
                            }
                            sendMessage(MSG_GET_BLACK_LIST_OK, result);
                            return;
                        }
                        sendMessage(MSG_GET_BLACK_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 黑名单操作
     * @param blackUserId
     */
    public void doBlackUser(final long blackUserId) {
        NetManager.getInstance(mContext).doBlackUser(
                blackUserId,
                0,
                new OnResponseListener<Boolean>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_BLACK_USER_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                        if (isOK) {
                            sendMessage(MSG_BLACK_USER_OK, errorCode, 0, errorMsg);
                            return;
                        }
                        sendMessage(MSG_BLACK_USER_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 取消拉黑
     * @param blackUserId
     */
    public void doUnBlackUser(final long blackUserId) {
        NetManager.getInstance(mContext).doBlackUser(
                blackUserId,
                1,
                new OnResponseListener<Boolean>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_UNBLACK_USER_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                        if (isOK) {
                            sendMessage(MSG_UNBLACK_USER_OK, errorCode, 0, errorMsg);
                            return;
                        }
                        sendMessage(MSG_UNBLACK_USER_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 获取帖子详情
     * @param subjectId
     */
    public void doLoadSubjectDetail(final long subjectId) {
        NetManager.getInstance(mContext).doGetForumDetail(subjectId, new OnResponseListener<SnsSubjectDetail>() {
            @Override
            public void onComplete(boolean isOK, SnsSubjectDetail result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_FORUMS_DETAIL_OK, result);
                    return;
                }
                sendMessage(MSG_GET_FORUMS_DETAIL_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_FORUMS_DETAIL_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取帖子评论列表
     * @param subjectId
     */
    public void doLoadSubjectCommentList(final long subjectId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetCommentList(subjectId, mPageIndex, mPageSize, new OnResponseListener<SnsCommentInfoList>() {
            @Override
            public void onComplete(boolean isOK, SnsCommentInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_FORUMS_COMMENTS_OK, result);
                    return;
                }
                sendMessage(MSG_GET_FORUMS_COMMENTS_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_FORUMS_COMMENTS_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 用户信息
     * @param targetUserId
     */
    public void doGetUserInfo(final long targetUserId) {
        NetManager.getInstance(mContext).doGetUserInfo(targetUserId, new OnResponseListener<UserInfo>() {
            @Override
            public void onComplete(boolean isOK, UserInfo result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_USER_INFO_OK, result);
                    return;
                }
                sendMessage(MSG_GET_USER_INFO_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_USER_INFO_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 用户相册
     * @param userId
     */
    public void doGetUserAlbums(final long userId) {
        NetManager.getInstance(mContext).doGetAlbums(userId, new OnResponseListener<ImageInfoList>() {
            @Override
            public void onComplete(boolean isOK, ImageInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_USER_ALBUMS_OK, result);
                    return;
                }
                sendMessage(MSG_GET_USER_ALBUMS_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_USER_ALBUMS_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 关注话题
     */
    public void doFollowTopic(final TextView v, final SnsTopicInfo snsTopicInfo) {
        NetManager.getInstance(mContext).doFocusTopic(snsTopicInfo.topicId, 0, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_FOLLOWED_TOPIC_OK, result);
                    snsTopicInfo.isAttentioned = true;
                    v.setText(mContext.getString(R.string.label_btn_cancel_attention));
                } else {
                    sendMessage(MSG_FOLLOWED_TOPIC_KO, errorCode, 0, errorMsg);
                    LocalUtils.showToast(mContext, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_FOLLOWED_TOPIC_KO, errorCode, 0, errorMessage);
                LocalUtils.showToast(mContext, errorMessage);
            }
        });
    }
    /**
     *  取消关注话题
     */
    public void doUnFollowTopic(final TextView v,final SnsTopicInfo snsTopicInfo) {
        NetManager.getInstance(mContext).doFocusTopic(snsTopicInfo.topicId, 1, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK && result) {
                    sendMessage(MSG_UNFOLLOWED_TOPIC_OK, result);
                    snsTopicInfo.isAttentioned = false;
                    v.setText(mContext.getString(R.string.label_btn_attention));
                } else {
                    sendMessage(MSG_UNFOLLOWED_TOPIC_KO, errorCode, 0, errorMsg);
                    LocalUtils.showToast(mContext, errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_UNFOLLOWED_TOPIC_KO, errorCode, 0, errorMessage);
                LocalUtils.showToast(mContext, errorMessage);
            }
        });
    }

    /**
     *
     * @param topicId
     */
    public void doLoadTopicDetailByTopicId(final long topicId){
        NetManager.getInstance(mContext).doTopicDetail(topicId, new OnResponseListener<SnsTopicInfo>() {
            @Override
            public void onComplete(boolean isOK, SnsTopicInfo result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_TOPIC_DETAIL_OK, result);
                    return;
                }
                sendMessage(MSG_GET_TOPIC_DETAIL_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_TOPIC_DETAIL_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取话题详情
     * @param topicId
     * @param page
     * @param row
     */
    public void doGetCommentListByTopicId(final long topicId, final int page, final int row) {
        NetManager.getInstance(mContext).doGetCommentListByTopicId(topicId, page, row, new OnResponseListener<SnsSubjectDetailList>() {
            @Override
            public void onComplete(boolean isOK, SnsSubjectDetailList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_TOPIC_COMMENT_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_TOPIC_COMMENT_LIST_KO, errorCode, 0, null);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_TOPIC_COMMENT_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 从本地缓存加载数据
     */
    public void loadCacheData() {
        mCacheManager.loadFromCache();
    }


    /**
     * 数所缓存管理
     */
    private class CacheManager {
        public static final String HEALTH_CIRCLE_CACHE = "HEALTH_CIRCLE_CACHE";
        private String key_subjects;
        LocalJsonCache<SnsSubjectDetailList> nNearByForumsCache;
        private CacheManager(String cachetag) {
            key_subjects = TAG + HEALTH_CIRCLE_CACHE + cachetag;

            nNearByForumsCache = new LocalJsonCache<SnsSubjectDetailList>(mContext);
            nNearByForumsCache.setCallback(new LocalJsonCache.Callback<SnsSubjectDetailList>() {
                @Override
                public void onSave(String path) {
                }

                @Override
                public void onParse(String path, SnsSubjectDetailList t) {
                    if (t != null) {
                        sendMessage(MSG_NEAR_BY_FORUMS_OK, MSG_DATA_FROM_CACHE, 0, t);
                    }
                }
            });
        }

        public void saveSnsSubjectDetailList(SnsSubjectDetailList t) {
            nNearByForumsCache.save(key_subjects, t);
        }

        public void loadFromCache() {
            nNearByForumsCache.load(key_subjects, SnsSubjectDetailList.class);
        }
    }
//
//    public Dialog retrydialog = null;
//    /**
//     *重新加载话题列表
//     */
//    public void sendReloadSubject(){
//        sendMessage(MSG_RELOAD_SUBJECTS);
//    }
//
//    /**
//     * 重新加载我的粉丝列表
//     */
//    public void sendReloadFans(){
//        sendMessage(MSG_RELOAD_FANS);
//    }
//    /**
//     *重新加载我关注的列表
//     */
//    public void sendReloadFollowedFans(){
//        sendMessage(MSG_RELOAD_FOLLOWED_FANS);
//    }

    /**
     * 获取推荐话题列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetRecommTopicList(final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetTopicList(
                2,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsTopicInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(CommunityController.MSG_GET_RECOMM_TOPIC_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsTopicInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsTopicInfoList();
                            }
                            sendMessage(CommunityController.MSG_GET_RECOMM_TOPIC_LIST_OK, result);
                            return;
                        }
                        sendMessage(CommunityController.MSG_GET_RECOMM_TOPIC_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 获取热门话题列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetHotTopicList(final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetTopicList(
                1,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsTopicInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(CommunityController.MSG_GET_HOT_TOPIC_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsTopicInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsTopicInfoList();
                            }
                            sendMessage(CommunityController.MSG_GET_HOT_TOPIC_LIST_OK, result);
                            return;
                        }
                        sendMessage(CommunityController.MSG_GET_HOT_TOPIC_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }
}
